package com.dmooo.yxmmerchant.view.model;

import android.content.Context;

import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.mywidget.LoadingDialog;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.dmooo.yxmmerchant.view.contract.UserSetContract;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import cz.msebera.android.httpclient.Header;

public class UserSetModel implements UserSetContract.UserSetMdl {
    private Context context;
    LoadingDialog loadingDialog;
    public UserSetModel(Context context){
        this.context=context;
        loadingDialog=new LoadingDialog(context);
        loadingDialog.setTitle("上传中");
    }
    @Override
    public void editUserMsg(Map<String, Object> map, HttpOnNextListener listener) {
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"修改中"),map);
        HttpManager.getInstance().editUserMsg(post);
    }

    @Override
    public void editAvatar(File file, String token, final HttpOnNextListener listener) {
        try {
            RequestParams requestParams = new RequestParams();
            requestParams.put("user_avatar", file);
            AsyncHttpClient client1 = new AsyncHttpClient();
            client1.addHeader("token", token);
            requestParams.put("token", token);
            client1.post(Config.APP_INTERFACE_BASE_URL + "/app.php?c=User&a=editUserAvatar", requestParams, new TextHttpResponseHandler() {

                @Override
                public void onStart() {
                    super.onStart();
                    loadingDialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    listener.onFail(responseString);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    LogUtil.e("backdata",responseString);
                    try {
                        JSONObject object1=new JSONObject(responseString);
                        if(object1.getInt("code")==0){
                            listener.onNext("");
                        }else{
                            listener.onFail(object1.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }catch (FileNotFoundException e){
            LogUtil.e("backdata",e.toString());
        }
    }

    @Override
    public void changePwd(String token, String oldPwd, String newPwd, String newPwd2, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("oldpwd",oldPwd);
        map.put("pwd1",newPwd);
        map.put("pwd2",newPwd2);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"修改中"),map);
        HttpManager.getInstance().changePwd(post);
    }

    @Override
    public void userAuth(String token, File img_positive, File img_opposite, String turename, String id_num,final HttpOnNextListener listener) {
        try {
            RequestParams requestParams = new RequestParams();
            requestParams.put("img_positive", img_positive);
            requestParams.put("img_opposite", img_opposite);
            requestParams.put("token", token);
            requestParams.put("truename", turename);
            requestParams.put("id_num", id_num);
            AsyncHttpClient client1 = new AsyncHttpClient();
            client1.addHeader("token", token);
            client1.post(Config.APP_INTERFACE_BASE_URL + "/app.php?c=UserAuth&a=addUserAuth", requestParams, new TextHttpResponseHandler() {

                @Override
                public void onStart() {
                    super.onStart();
                    loadingDialog.show();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    loadingDialog.dismiss();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    listener.onFail(responseString);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    LogUtil.e("backdata",responseString);
                    try {
                        JSONObject object1=new JSONObject(responseString);
                        if(object1.getInt("code")==0){
                            listener.onNext("");
                        }else{
                            listener.onFail(object1.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }catch (FileNotFoundException e){
            LogUtil.e("backdata",e.toString());
        }
    }
}
